//
//  PokerHand.m
//  PokerHand
//
//  Created by mrbradle on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PokerHand.h"


@implementation PokerHand

@synthesize cards;

-(int)check {
    
    /*
     0 = nothing
     1 = pair
     2 = two pair
     3 = 3 of a kind
     4 = straight
     5 = flush
     6 = full house
     7 = 4 of a kind
     8 = straight flush
     9 = royal flush
    */
    
    int hand = 0;
    
    //First we sort all cards by value.
    
    int values[5];
    int suits[5];
    for (int i = 0; i < 5; i++) {
        Card* temp = [cards objectAtIndex:i];
        values[i] = temp.value;
        suits[i] = temp.suit;
    }
    
    //Use insertion sort
    
    for (int i = 0; i < 5; i++) {
        for (int j = i; j < 5; j++) {
            if (values[j] < values[i]) {
                int tempValue = values[i];
                int tempSuit = suits[i];
                values[i] = values[j];
                suits[i] = suits[j];
                values[j] = tempValue;
                suits[j] = tempSuit;
            }
        }
    }
    
    //Find what hand we have!
    
    if (values[0] == values[1] && values[1] == values[2] && values[2] == values[3] ||
        values[1] == values[2] && values[2] == values[3] && values[3] == values[4]) return 7; //4 of a kind cannot be improved
    
    if (suits[0] == suits[1] && suits[1] == suits[2] && suits[2] == suits[3] && suits[3] == suits[4]) hand = 5; //flush
    
    if (values[0]+1 == values[1] &&
        values[1]+1 == values[2] &&
        values[2]+1 == values[3] &&
        values[3]+1 == values[4] ||
        values[0]+1 == values[1] &&
        values[1]+1 == values[2] &&
        values[2]+1 == values[3] &&
        values[0] == 0 && values[4] == 12) {
        if (values[4] == 12 && values[3] == 11 && hand == 5) return 9;
        if (hand == 5) return 8;
        else return 4; //straight, straight flush, or royal flush
    }
    
    if (hand == 5) return 5; //at this point we don't have a straight, so if we have a flush that's all we have
    
    bool three = NO;
    bool pair = NO;
    int alreadyUsed = -1; //We might have one or more of these, so we need to keep track of if we've found one and what it is
    for (int i = 0; i < 3; i++) {
        if (values[i] == values[i+1] && values[i] == values[i+2]) {
            three = YES;
            alreadyUsed = values[i];
        }
    }
    
    for (int i = 0; i < 4; i++) {
        if (values[i] == values[i+1] && values[i] != alreadyUsed) {
            if (three == YES) return 6; //three of a kind and a pair = full house
            else if (pair == YES) return 2; //two pairs
            else pair = YES;
        }
    }
    if (three == YES) return 3; //if we have 3 of a kind without anything else
    if (pair == YES) return 1; //if we have a pair without anything else
    
    return 0; //we found nothing, so you might as well just fold this hand because there's no way you'll win with it
    
}

@end
